import modelAbstract from "./modelAbstract";
import {images} from "../service/image"
import {directionEnum} from "../directionEnum";
import config from '../config'
import util from '../util'
import _ from "lodash"
import tank from "../canvas/tank";
export default class extends modelAbstract implements IModel{
    canvas: ICanvas = tank;
    name: string = "tank";
    render(): void {
        this.move();
        if(_.random(20) == 1){
            this.direction = directionEnum.bottom
        }
    }
    protected move():void{
        while(true){
            let x = this.x;
            let y = this.y;
            switch(this.direction){
                case directionEnum.top:
                    y --;
                    break;
                case directionEnum.right:
                    x ++;
                    break;
                case directionEnum.bottom:
                    y ++;
                    break;
                case directionEnum.left:
                    x --;
                    break;
            }
            if(util.isModelTouch(x,y) || util.isCanvasTouch(x,y)){
                this.randomDirection();
            }else{
                this.x = x;
                this.y = y;
                break;
            }
        }
        super.draw();
    }
    image(){
        let image = this.name + _.upperFirst(this.direction);
        return images.get(image as keyof typeof config.images)!
    }
}